#include "Device/NetPointControlDevice.h"
#include "Command/PointProtocol/UdpPointSetSwitchCmd.h"
#include "Command/PointProtocol/ControlPointCmd.h"
#include "PointDevice.h"
#include "File/Xml/OtherEquipmentXml.h"
#include "Command/CloudProtocol/HeartBestcmd.h"
NetPointControlDevice::NetPointControlDevice(QObject *parent) :
    BaseInterface<NetPointControlDevice>(parent)
{
    qDebug()<<"NetPointControlDevice runing-1";
    m_pInstance = this;

    pointProtocol = new PointProtocol(this);

    cloudProtocol = new CloudProtocol(this);

    this->registerProtocol(cloudProtocol);
    this->registerProtocol(pointProtocol);
    //qDebug()<<"NetPointControl ture----------------------------";
 // registerCmdHandler(UdpPointRelay::FunctionCode,UdpPointRelay::staticMetaObject, &NetPointControlDevice::Function);
    registerFuncHandler(ControlPointFunc::FunctionCode, &NetPointControlDevice::controlPointFuncHandle);

    registerCmdHandler(HeartBestCmd::FunctionCode,HeartBestCmd::staticMetaObject, &NetPointControlDevice::heartBestHandled);
    registerCmdHandler(UdpPointSetSwitchCmd::FunctionCode,UdpPointSetSwitchCmd::staticMetaObject,&NetPointControlDevice::UDPSetSwitchReply);
 //   registerCmdHandler(ControlPointCmd::FunctionCode,ControlPointCmd::staticMetaObject, &NetPointControlDevice::controlPointCmdHandle);
   qDebug()<<"NetPointControlDevice runing-2";
}

void NetPointControlDevice::controlPointCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);

    ControlPointCmd *setControlPointCmd = qobject_cast<ControlPointCmd*>(sharedCmd.data());

    int address = setControlPointCmd->address();
    int value = setControlPointCmd->value();

    ControlPointFunc *pointControlFunc = new ControlPointFunc();
    ControlPointStruct sourcePoint;
    sourcePoint.setAddress(address);
    sourcePoint.setValue(value);
    QList<ControlPointStruct> sourcePointList;
    sourcePointList.append(sourcePoint);
    pointControlFunc->setSourceDevice(this->name());
    pointControlFunc->setSourcePointList(sourcePointList);

    QSharedPointer<BaseFunc> sharedPointControlFunc(pointControlFunc);
   // emit transferFuncSignal(sharedPointControlFunc);
    PortRelayUDPpoint(source,sharedCmd);
}

void NetPointControlDevice::UDPSetSwitchReply(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
   OtherEquipmentXml *Equipmentxml=OtherEquipmentXml::getInstance();
   UdpPointSetSwitchCmd *UdpData =qobject_cast<UdpPointSetSwitchCmd*>(sharedCmd.data());

   quint16 getAddr=UdpData->address();
   quint16 value=0x0001;
   pointProtocol = new PointProtocol(this);
   if(Equipmentxml->modify(source,sharedCmd)==true)
   {
     quint16 function=0x0f82;
     QByteArray data = pointProtocol->getPointData(function,getAddr,value);
     emit sendDataSignal(source,data);
   }

}
bool NetPointControlDevice::Function(QList<PointSwitchUdpStruct> list,quint16 addr,quint16 value)
{
    pointProtocol = new PointProtocol(this);
    //qDebug()<<"now in function";
    foreach (const PointSwitchUdpStruct &pointSwitchUdpStruct, list)
    {
        quint16 getAddr =pointSwitchUdpStruct.Addr();
        QString getIP   =pointSwitchUdpStruct.IP();
        PortStruct target;
        target.setType("UDP");
        target.setName("NetPointDeviceUDP");
        target.setIp(getIP);
        target.setPort(20000);
        if(getAddr == addr)
        {
            m_source.setIp(getIP);
            quint16 function=0x0f80;
            QByteArray data = pointProtocol->getPointData(function,addr,value);
            sendDataSignal(target,data);
        }
    }
    return true;
}

void NetPointControlDevice::controlPointFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    ControlPointFunc *pointControlFunc = qobject_cast<ControlPointFunc*>(sharedFunc.data());
    QList<ControlPointStruct> controlPointList;
    qDebug()<<"SourceDevice-name="<<pointControlFunc->sourceDevice();
    qDebug()<<"this-name="<<this->name();
    if(pointControlFunc->sourceDevice() != this->name())
    {
        controlPointList.append(pointControlFunc->sourcePointList());
    }
    controlPointList.append(pointControlFunc->controlPointList());

    if(controlPointList.isEmpty())
    {
        return;
    }


    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }

    for(int i=0; i<controlPointList.size(); i++)
    {
        ControlPointStruct controlPoint = controlPointList.at(i);
        int address = controlPoint.address();
        int value = controlPoint.value();
        if(address==0x00)
        {
            return;
        }
        ControlPointCmd *setControlPointCmd = new ControlPointCmd();

        setControlPointCmd->setAddress(address);
        setControlPointCmd->setValue(value);

        QSharedPointer<BaseCmd> sharedSetControlPointCmd(setControlPointCmd);
        controlPointCmdHandle(target,sharedSetControlPointCmd);
     //   sendCmd(target,sharedSetControlPointCmd);

    }
}

void NetPointControlDevice::SetSource(const PortStruct source)
{
    m_source=source;
}

void NetPointControlDevice::PortRelayUDPpoint(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    ControlPointCmd *setControlPointCmd = qobject_cast<ControlPointCmd*>(sharedCmd.data());
    int address = setControlPointCmd->address();
    int value = setControlPointCmd->value();
    OtherEquipmentXml *otherEquipmentXml = OtherEquipmentXml::getInstance();

   // foreach (const PointSwitchUdpStruct &pointData, otherEquipmentXml->m_pointSwitchUdpList)
  //  {
  //      qDebug()<<"getDevice ID="<<pointData.Name();
  //      qDebug()<<"SetAddr="<<pointData.Addr();
  //      qDebug()<<"IP="<<pointData.IP();
  //  }

    Function(otherEquipmentXml->m_pointSwitchUdpList,address,value);

}

void NetPointControlDevice::heartBestHandled(PortStruct target,QSharedPointer<BaseCmd> sharedCmd)
{
    //qDebug()<<"hello my friends";
    Q_UNUSED(target);
    Q_UNUSED(sharedCmd);
    //qDebug()<<"HeartBest now -1";
  HeartBestList *HeartBestlist=HeartBestList::GetInstance();
 // QList<HeartBestData> HeartBestDatalist;
  HeartBestCmd *Heartbeatcmd=qobject_cast<HeartBestCmd*>(sharedCmd.data());
  HeartBestData HeartBestdata;
  HeartBestdata.SetMAC(Heartbeatcmd->MAC());
  HeartBestdata.SetID(Heartbeatcmd->EquimentID());
  HeartBestdata.SetErrornum(Heartbeatcmd->Errornum());
  HeartBestdata.SetProgramNum(Heartbeatcmd->ProgramNum());
  HeartBestdata.SetTypenum(Heartbeatcmd->Typenum());
  foreach (const HeartBestData &meta, HeartBestlist->m_HeartBestDataList)
  {
    if(meta.MAC()==HeartBestdata.MAC())
    {
        //qDebug()<<meta.MAC().data();
        return;
    }
    //qDebug()<<meta.MAC().data();
  }

  HeartBestlist->m_HeartBestDataList.append(HeartBestdata);

  //qDebug()<<"HeartBest now -2";
}

